package com.jianganming.netty.tcp;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.nio.charset.Charset;

public class TcpClient {
    public static void main(String[] args) {
        EventLoopGroup group = new NioEventLoopGroup();
        Bootstrap bootstrap = new Bootstrap();
        try{
            ChannelFuture future = bootstrap.group(group)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel socketChannel) throws Exception {
                            ChannelPipeline pipeline = socketChannel.pipeline();
                            pipeline.addLast(new MessageDecoder());
                            pipeline.addLast(new MessageEncoder());
                            pipeline.addLast(new SimpleChannelInboundHandler<MessageProtocol>() {
                                private int count;
                                @Override
                                public void channelActive(ChannelHandlerContext ctx) throws Exception {
                                    for (int i = 0; i < 10; i++) {
                                        String message = "hello_server";
                                        byte[] bytes = message.getBytes();
                                        MessageProtocol messageProtocol = new MessageProtocol();
                                        messageProtocol.setLen(bytes.length);
                                        messageProtocol.setContent(bytes);
                                        //ByteBuf byteBuf = Unpooled.copiedBuffer("hello_server", Charset.forName("utf-8"));
                                        ctx.writeAndFlush(messageProtocol);
                                    }
                                }

                                @Override
                                protected void channelRead0(ChannelHandlerContext channelHandlerContext, MessageProtocol messageProtocol) throws Exception {
                                    //byte[] bytes = new byte[byteBuf.readableBytes()];
                                    //byteBuf.readBytes(bytes);
                                    String message = new String(messageProtocol.getContent(), Charset.forName("utf-8"));
                                    System.out.println("客户端收到消息   "+message);
                                    System.out.println("客户端收到次数   "+(++count));
                                }

                                @Override
                                public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                                    cause.printStackTrace();
                                    count=0;
                                    ctx.close();
                                }
                            });
                        }
                    }).connect("127.0.0.1", 8889).sync();

            future.channel().closeFuture().sync();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            group.shutdownGracefully();
        }
    }
}
